home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 2,801 to 2,900 / aol-file-protocol-4400-2801-to-2900.zip / AOLDLs / C++ Files Library / Graphic Gems I, II & III (C_C++) / Graphics Gems C Code.sea / GemsI / Src / DigitalLine.c < prev    next >
Text File  |  1992-06-16  |  943b  |  55 lines

  1. /*
  2.  * Digital Line Drawing
  3.  * by Paul Heckbert
  4.  * from "Graphics Gems", Academic Press, 1990
  5.  */
  6.  
  7. /*
  8.  * digline: draw digital line from (x1,y1) to (x2,y2),
  9.  * calling a user-supplied procedure at each pixel.
  10.  * Does no clipping.  Uses Bresenham's algorithm.
  11.  *
  12.  * Paul Heckbert    3 Sep 85
  13.  */
  14.  
  15. #include "GraphicsGems.h"
  16.  
  17. digline(x1, y1, x2, y2, dotproc)
  18. int x1, y1, x2, y2;
  19. void (*dotproc)();
  20. {
  21.     int d, x, y, ax, ay, sx, sy, dx, dy;
  22.  
  23.     dx = x2-x1;  ax = ABS(dx)<<1;  sx = SGN(dx);
  24.     dy = y2-y1;  ay = ABS(dy)<<1;  sy = SGN(dy);
  25.  
  26.     x = x1;
  27.     y = y1;
  28.     if (ax>ay) {        /* x dominant */
  29.     d = ay-(ax>>1);
  30.     for (;;) {
  31.         (*dotproc)(x, y);
  32.         if (x==x2) return;
  33.         if (d>=0) {
  34.         y += sy;
  35.         d -= ax;
  36.         }
  37.         x += sx;
  38.         d += ay;
  39.     }
  40.     }
  41.     else {            /* y dominant */
  42.     d = ax-(ay>>1);
  43.     for (;;) {
  44.         (*dotproc)(x, y);
  45.         if (y==y2) return;
  46.         if (d>=0) {
  47.         x += sx;
  48.         d -= ay;
  49.         }
  50.         y += sy;
  51.         d += ax;
  52.     }
  53.     }
  54. }
  55.